import { defineStore } from 'pinia'
import { store } from '..'
import { useMessage } from '/@/hooks/web/useMessage'

interface BaseParam {
  count: number
  name?: string
  member: string[]
}

export const baseParamStore = defineStore({
  id: 'app-base',
  state: (): BaseParam => ({
    count: 0,
    name: '',
    member: ['张三'],
  }),
  getters: {
    getCount(): number {
      this.count = this.member.length
      return this.count
    },
  },
  actions: {
    increment() {
      this.count++
    },
    updateName(name: string) {
      this.name = name
    },
    addMember(name: string) {
      const set = new Set([...this.member])
      if (set.has(name)) {
        useMessage().createMessage.error('请勿重复添加')
      } else {
        this.member.push(name)
      }
    },
    deleteMeber(name: string) {
      const set = new Set([...this.member])
      if (set.has(name)) {
        set.delete(name)
        this.member = Array.from(set)
      } else {
        useMessage().createMessage.error('该对象不存在')
      }
    },
  },
})

// 需要在setup之外使用
export function useBaseStoreWithout() {
  return baseParamStore(store)
}
